package com.lance.core.launch.server;

import com.lance.core.launch.utils.NetUtil;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;


/**
 * @author lancecong
 * @version 1.0
 * @ClassName ServerInfo
 * @DESCRIPTION 所有非 lazy 单例 Bean 实例化完成后的回调方法
 * InitializingBean 执行阶段是 afterPropertiesSet；
 * SmartInitializingSingleton 执行阶段是 pre-instantiation；
 * SmartInitializingSingleton 的 afterSingletonsInstantiated 方法是在所有单例 bean 都已经被创建后执行的。
 * @create 2019-06-28 18:05
 **/
public class ServerInfo implements SmartInitializingSingleton {

    private final ServerProperties serverProperties;

    private String hostName;
    private String ip;
    private Integer port;

    @Autowired(required = false)
    public ServerInfo(ServerProperties serverProperties){
        this.serverProperties = serverProperties;
    }
    @Override
    public void afterSingletonsInstantiated() {
        this.hostName = NetUtil.getHostName();
        this.ip = NetUtil.getHostIp();
        this.port= serverProperties.getPort();

    }
}
